package org.mikhaylov.cp.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.mikhaylov.cp.service.db.entities.City;
import org.mikhaylov.cp.service.db.entities.Inventory;
import org.mikhaylov.cp.service.db.entities.InventoryType;
import org.mikhaylov.cp.service.db.entities.Region;
import org.mikhaylov.cp.service.db.entities.User;
import org.mikhaylov.cp.service.hibernate.HibernateManager;
import org.mikhaylov.cp.service.hibernate.HibernateUtil;

@Deprecated
public class Entry {

	public static void main(String[] args) throws ClassNotFoundException {
		System.out.println("[Entry]: Opening session");
		Class.forName(HibernateUtil.class.getName());
		Session session = HibernateUtil.getSessionFactory().openSession();

		System.out.println("[Entry]: Factory got");

		Transaction t = session.beginTransaction();

		/*System.out.println("[Entry]: Transaction begun");

		Region r = new Region();
		r.setName("Kharkivska");
		r.setCities(new HashSet<City>());

		City city = new City();
		city.setName("Kharkiv");
		city.setRegion(r);

		//session.save(r);

		r.getCities().add(city);

		//session.save(r);
		//session.save(city);

		User user = new User();
		user.setBalance(100.);
		user.setInventories(new HashSet<Inventory>());
		user.setLogin("user");
		user.setPassword("user");
		user.setTakes(new HashSet<Take>());
		user.setCity(city);

		//session.save(user);

		Criteria c = session.createCriteria(User.class, "user");
		c.createAlias("user.city", "city");
		c.createAlias("city.region", "region");
		c.add(Restrictions.eq("region.name", "Kharkivska"));

		System.out.println(c.list().size());

		for (Object o : c.list()) {
			System.out.println(o instanceof User);
		}

		InventoryType inventoryType = new InventoryType();
		inventoryType.setName("Board");
		inventoryType.setDescription("Nodesc");

		//session.save(inventoryType);

		Inventory inventory = new Inventory();
		inventory.setName("My bord");
		inventory.setPrice(100.);
		inventory.setInventoryType(inventoryType);
		inventory.setUser(user);
		inventory.setTakes(new HashSet<Take>());
		
		//session.save(inventory);
		
		user.getInventories().add(inventory);
		
		//session.save(user);
		 */
		InventoryType invt = (InventoryType) session.get(InventoryType.class, 1L);
		System.out.println(invt.getName());
		
		Inventory inv = (Inventory) session.get(Inventory.class, 1L);
		System.out.println(inv.getName());
		
		User user = (User) session.get(User.class, 1L);
		System.out.println(user.getLogin());
		
		Region r = (Region) session.get(Region.class, 1L);
		System.out.println(r.getName());
		
		City c = (City) session.get(City.class, 1L);
		System.out.println(c.getName());
		
		System.out.println(session.createCriteria(Inventory.class, "inventory").list().size());
		
		System.out.println(HibernateManager.getInstance()
				.getInventories(invt, 20., 120., r, c).size());
		//.getInventories(inventoryType, 20., 120., r, city).size());

		t.rollback();
		
		//System.out.println(HibernateManager.getInstance()
		//		.getInventories(inventoryType, null, null, null, null).size());
		System.out.println("[Entry]: Transaction commited");
	}

}
